package com.hxkcloud.patient.activitys;

import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.utils.StringUtil;

import java.util.regex.Pattern;

/**
 * Created by ray_lei on 2016/7/10.
 */
public class ChangePwActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout titleLayout;
    private ImageView backIV;
    private TextView titleTV;

    private EditText currentpwET;
    private EditText newpwET;
    private EditText againET;

    private Button saveBtn;

    private boolean oldInput = false;
    private boolean newpwInput = false;
    private boolean againpwInput = false;

    private ObjectResultBean<String> resultBean;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_UPDATEPW_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<String>) msg.obj;
                    toastMsg(resultBean.getMessage());
                    finish();

                }

                break;
            case MessageCode.NETWORKCODE_UPDATEPW_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_chagepw);
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        titleLayout = (LinearLayout) findViewById(R.id.ll_title);
        setTitlePadding(titleLayout);
        backIV = (ImageView) findViewById(R.id.iv_title_back);
        backIV.setVisibility(View.VISIBLE);
        titleTV = (TextView) findViewById(R.id.tv_title_title);
        titleTV.setText(getResources().getString(R.string.setting_text_changepw));

        currentpwET = (EditText) findViewById(R.id.changepw_et_currentpw);
        newpwET = (EditText) findViewById(R.id.changepw_et_newpw);
        againET = (EditText) findViewById(R.id.changepw_et_again);

        saveBtn = (Button) findViewById(R.id.changepw_btn_save);
        saveBtn.setEnabled(false);

        currentpwET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    oldInput = false;
                    saveBtn.setBackgroundResource(R.drawable.bt_grey);
                    saveBtn.setEnabled(false);
                } else {
                    oldInput = true;
                    if (newpwInput && againpwInput) {
                        saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        saveBtn.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        newpwET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    newpwInput = false;
                    saveBtn.setBackgroundResource(R.drawable.bt_grey);
                    saveBtn.setEnabled(false);
                } else {
                    newpwInput = true;
                    if (oldInput && againpwInput) {
                        saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        saveBtn.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        againET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    againpwInput = false;
                    saveBtn.setBackgroundResource(R.drawable.bt_grey);
                    saveBtn.setEnabled(false);
                } else {
                    againpwInput = true;
                    if (newpwInput && oldInput) {
                        saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        saveBtn.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected void initListener() {
        backIV.setOnClickListener(this);
        saveBtn.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.changepw_btn_save:
                if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient)) {
                    StringUtil.zhugeTrack(mContext, "设置中修改密码页面中保存按钮", "设置中修改密码页面中保存按钮点击数");
                }

                String currentpw = currentpwET.getText().toString();
                String newpw = newpwET.getText().toString();
                String again = againET.getText().toString();
                if (TextUtils.isEmpty(currentpw)) {
                    toastMsg(this.getResources().getString(R.string.pw_not_empty));
                    return;
                }
                if (TextUtils.isEmpty(newpw)) {
                    toastMsg(this.getResources().getString(R.string.pw_not_empty));
                    return;
                }
                if (TextUtils.isEmpty(again)) {
                    toastMsg(this.getResources().getString(R.string.pw_not_empty));
                    return;
                }
                if (!newpw.equals(again)) {
                    toastMsg(this.getResources().getString(R.string.newpw_equals_againpw));
                    return;
                }
//                if(currentpw.length() < 6 ){
//                    toastMsg(this.getResources().getString(R.string.input_right_pw));
//                    return;
//                }
                if (currentpw.equals(newpw)) {
                    toastMsg(this.getResources().getString(R.string.oldpw_notequals_newpw));
                    return;
                }
//                if(newpw.length() < 6 ){
//                    toastMsg(this.getResources().getString(R.string.input_right_pw));
//                    return;
//                }
//                if(again.length() < 6 ){
//                    toastMsg(this.getResources().getString(R.string.input_right_pw));
//                    return;
//                }
//                if (!Pattern.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,11}$", newpw)) {
                if (!Pattern.matches("^[a-zA-Z0-9]{6,11}$", newpw)) {
                    toastMsg(R.string.input_right_pw);
                    return;
                }

                loading();
                YiJiaHuNetWorkUtils.updatePassword(currentpw, newpw, handler);

                break;
        }
    }
}
